package com.zut.subject.domain.handler.subject;

import com.zut.subject.common.enums.IsDeletedFlagEnum;
import com.zut.subject.common.enums.SubjectTypeEnum;
import com.zut.subject.domain.convert.ConvertSubjectType;
import com.zut.subject.domain.entity.SubjectInfoBo;
import com.zut.subject.domain.entity.SubjectOptionAnswerBo;
import com.zut.subject.infra.basic.entity.SubjectMultiple;
import com.zut.subject.infra.basic.entity.SubjectRadio;
import com.zut.subject.infra.basic.service.SubjectRadioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Component
public class RadioSubjectTypeHandlerImpl implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;


    @Override
    public SubjectTypeEnum subjectEnum() {
        return SubjectTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if (log.isInfoEnabled()){
            log.info("传入单选handler为：{}",subjectInfoBo);
        }

        //单选题逻辑
        List<SubjectOptionAnswerBo> optionList = subjectInfoBo.getOptionList();
        if (log.isInfoEnabled()){
            log.info("转化为选项List为：{}",optionList);
        }

        List<SubjectRadio> subjectRadios = ConvertSubjectType.INSTANCE.toListSubjectRadio(optionList);
        if (log.isInfoEnabled()){
            log.info("传入单选数据库的数据为：{}",subjectRadios);
        }
        //给单选题赋值题目id
        subjectRadios.forEach(subjectRadio -> {
            subjectRadio.setSubjectId(subjectInfoBo.getId());
            subjectRadio.setIsDelete(IsDeletedFlagEnum.UN_DELETED.getCode());
        });
        subjectRadioService.insertBatch(subjectRadios);


    }

    @Override
    public SubjectInfoBo querySubjectOption(Integer subjectId) {
        List<SubjectRadio> subjectRadioList = subjectRadioService.querySubjectOption(subjectId);
        List<SubjectOptionAnswerBo> Bo = ConvertSubjectType.INSTANCE.soaRadio(subjectRadioList);

        SubjectInfoBo subjectInfoBo =new SubjectInfoBo();
        subjectInfoBo.setOptionList(Bo);
        return subjectInfoBo;
    }
}
